<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head>
<body>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap/css/default.css">

<!-- Custom styles for this template -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/dashboard.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/adminnav.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap/css/style.css">

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.js"></script>
<script src="${pageContext.request.contextPath}/static/js/functions.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="${pageContext.request.contextPath}/static/css/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/static/css/bootstrap/js/prettify.min.js"></script>
<script src="${pageContext.request.contextPath}/static/css/bootstrap/js/multiselect.min.js"></script>

<script type="text/javascript">
    $(document).ready(
        function () {
            var currentPage = ${page.currentPage};
            var totalPage = ${page.totalPage};

            $("#pagination1").pagination({
                currentPage: currentPage,
                totalPage: totalPage,
                callback: function (current) {//current 当前页
                    var info = $("#pagination1").pagination("getPage");
                    location.href = "${pageContext.request.contextPath}/order/orderList?status=${orderQo.status}&oid=${orderQo.oid}&username=${orderQo.username}&startDate=<fmt:formatDate value="${orderQo.startDate}" pattern="yyyy-MM-dd"/>&endDate=<fmt:formatDate value="${orderQo.startDate}" pattern="yyyy-MM-dd"/>&sort=${orderQo.sort}&currentPage=" + info.current;
                }
            });
        }
    );



    function selOrdersBut(formName, cbName) {
        if ($("#selAllOrders").is(":checked"))
            selectAll(formName, cbName);
        else
            unSelectAll(formName, cbName);
    }

    function check() {
//        if (!validateCheckbox("orderIds", "请至少选中一个订单"))
//            return false;
//        else
//            return confirm("确实要删除选中的所有订单吗？");
        return confirm("确实要删除选中的订单吗？");
    }

    function handleOrder(orderId, status, msg, e) {
        if (confirm(msg)) {
            var os = $(e.target).parent().siblings(".orderStatus");

            $.post("${pageContext.request.contextPath}/order/handleOrderStatus", {
                oid: orderId,
                status: status
            }, function (result) {
                if (result) {
                    if (result.type == "success") {
                        var tdTag = $(e.target).parent();
                        tdTag.html("");
                        if (result.status == -3) {
                            os.html("交易关闭");
                        }
                        else if (result.status == -2) {
                            os.html("退款成功");
                        }
                        else if (result.status == -6) {
                            os.html("退货中");
                            tdTag.html("<a href=\"#\" onclick=\"handleOrder(" + orderId + ",8,'确定收到这个订单退货吗？',event);\">确认收到退货</a>");
                        }
                        else if (result.status == -4) {
                            os.html("退货成功");
                        }

                        $("#msgTitle").html("操作成功");
                        $("#msgBody").html("订单操作成功");
                        $("#msgModal").modal();
                    } else if (result.type == "failure") {
                        $("#msgTitle").html("操作失败");
                        $("#msgBody").html("抱歉，目前的订单状态无法进行此操作");
                        $("#msgModal").modal();
                    }
                    else {
                        $("#msgTitle").html("操作失败");
                        $("#msgBody").html("操作订单失败");
                        $("#msgModal").modal();
                    }
                }
            }, "json");
        }
    }

    function sendGoods(orderId, status, e) {
        $('#postInfoFormModal').modal();
        $("#ok").click(function () {
            var os = $(e.target).parent().siblings(".orderStatus");

            var orderPostname = $("#orderPostname").val();
            var orderPostcode = $("#orderPostcode").val();
            $.post("${pageContext.request.contextPath}/order/sendGoods", {
                oid: orderId,
                status: status,
                postCompany: orderPostname,
                postCode: orderPostcode
            }, function (result) {
                if (result) {
                    if (result.type == "success") {
                        if (result.status == 4) {
                            os.html("已发货");
                            $(e.target).parent().html("");
                        }
                        $("#msgTitle").html("操作成功");
                        $("#msgBody").html("订单操作成功");
                        $("#msgModal").modal();
                    } else if (result.type == "failure") {
                        $("#msgTitle").html("操作失败");
                        $("#msgBody").html("抱歉，目前的订单状态无法进行此操作");
                        $("#msgModal").modal();
                    }
                    else {
                        $("#msgTitle").html("操作失败");
                        $("#msgBody").html("操作订单失败");
                        $("#msgModal").modal();
                    }
                }
            }, "json");
        });
    }
</script>

<c:import url="../common/adminTopNav.jsp"/>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">

            <c:import url="../common/adminLeftNav.jsp"/>

        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <%--content--%>
            <div class="row">
                <div class="col-md-12">

                    <form class="form-inline" role="form" id="searchForm" method="post"
                          action="${pageContext.request.contextPath}/order/orderList">
                        <div class="form-group">
                            <label for="orderStatus"> </label>
                            <select class="form-control input-sm" name="status" id="orderStatus">
                                <option value="" ${empty orderQo?'selected':''}>---所有状态---</option>

                                <option value="0" ${orderQo.status==0?'selected':''}>等待付款</option>

                                <option value="1" ${orderQo.status==1?'selected':''}>未发货</option>

                                <option value="-1" ${orderQo.status==-1?'selected':''}>申请退款中</option>

                                <option value="-2" ${orderQo.status==-2?'selected':''}>退款成功</option>

                                <option value="2" ${orderQo.status==2?'selected':''}>已发货</option>

                                <option value="3" ${orderQo.status==3?'selected':''}>交易成功</option>

                                <option value="-5" ${orderQo.status==-5?'selected':''}>申请退货中</option>

                                <option value="-6" ${orderQo.status==-6?'selected':''}>退货中</option>

                                <option value="-4" ${orderQo.status==-4?'selected':''}>退货成功</option>

                                <option value="-3" ${orderQo.status==-3?'selected':''}>交易关闭</option>

                            </select>
                        </div>
                        <div class="form-group">
                            <label for="orderCode"> 订单号</label>
                            <input class="form-control input-sm" name="oid" id="orderCode" value="${orderQo.oid}" placeholder="订单号"/>
                        </div>
                        <div class="form-group">
                            <label for="userName"> 用户名</label>
                            <input class="form-control input-sm" name="username" id="userName"
                                   value="${orderQo.username}"
                                   placeholder="用户名"/>
                        </div>
                        <div class="form-group">
                            <label for="orderDate"> 下单日期 </label>
                            <input class="form-control input-sm" name="startDate" id="startDate"
                                   value="<fmt:formatDate value="${orderQo.startDate}" pattern="yyyy-MM-dd"/>"
                                   type="date"
                                   placeholder="起始日期"/>-
                            <input class="form-control input-sm" name="endDate" id="endDate"
                                   value="<fmt:formatDate value="${orderQo.endDate}" pattern="yyyy-MM-dd"/>" type="date"
                                   placeholder="终止日期"/>
                        </div>
                        <button class="btn btn-primary btn-sm" type="submit">搜索</button>
                    </form>
                </div>

            </div>

            <h2 class="sub-header">订单信息列表</h2>


            <div class="table-responsive ">
                <form name="orderForm" method="post"
                      action="${pageContext.request.contextPath}/order/delOrderByIds"
                      onsubmit="return check()">
                    <div class="row">
                        <div class="col-md-3">
                            <button class="btn btn-primary" type="submit">批量删除</button>
                        </div>
                        <div class="col-md-2 col-md-offset-7">
                            <div class="dropdown " id="dropdown">
                                <button class="btn btn-default dropdown-toggle btn-sm " type="button" id="dropdownMenu1"
                                        data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"
                                        onclick="dropdown()">
                                    排序 <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">

                                    <li>
                                        <a href="${pageContext.request.contextPath}/order/orderList?status=${orderQo.status}&oid=${orderQo.oid}&username=${param.username}&startDate=${param.startDate}&endDate=${param.endDate}&sort=1">按订单日期排序</a>
                                    </li>
                                    <li>
                                        <a href="${pageContext.request.contextPath}/order/orderList?status=${orderQo.status}&oid=${orderQo.oid}&username=${param.username}&startDate=${param.startDate}&endDate=${param.endDate}&sort=2">按订单状态排序</a>
                                    </li>
                                    <li>
                                        <a href="${pageContext.request.contextPath}/order/orderList?status=${param.status}&oid=${param.oid}&username=${param.username}&startDate=${param.startDate}&endDate=${param.endDate}&sort=3">按用户名排序</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="selAllOrders"
                                       onchange="selOrdersBut('orderForm', 'orderIds')"/> 全选
                            </th>
                            <th>#</th>
                            <th>订单号</th>
                            <th>用户名</th>
                            <th>订单状态</th>
                            <th>运费</th>
                            <th>下单日期</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${page.data}" var="ou" varStatus="status">
                            <tr>
                                <td><input type="checkbox" name="oids" value="${ou.oid}"/></td>
                                <td>${status.count}</td>
                                <td>
                                    <a href="${pageContext.request.contextPath}/order/getOrderDetail?oid=${ou.oid}">${ou.oid}</a>
                                </td>
                                <td>${ou.username}</td>
                                <td class="orderStatus">
                                    <c:if test="${ou.status==-3}">交易关闭</c:if>
                                    <c:if test="${ou.status==-2}">退款成功</c:if>
                                    <c:if test="${ou.status==-4}">退货成功</c:if>
                                    <c:if test="${ou.status==-1}">退款待通过</c:if>
                                    <c:if test="${ou.status==-5}">退货待通过</c:if>
                                    <c:if test="${ou.status==-6}">退货中</c:if>
                                    <c:if test="${ou.status==0}">等待付款</c:if>
                                    <c:if test="${ou.status==1}">未发货</c:if>
                                    <c:if test="${ou.status==2}">已发货</c:if>
                                    <c:if test="${ou.status==3}">交易成功</c:if>
                                </td>
                                <td>${ou.freight}</td>
                                <td><fmt:formatDate value="${ou.date}" pattern="yyyy-MM-dd"/></td>
                                <td>
                                    <c:if test="${ou.status==-1}">
                                        <a href="#" onclick="handleOrder('${ou.oid}',-2,'确定要同意退款吗？',event);">同意退款</a>
                                    </c:if>
                                    <c:if test="${ou.status==-5}">
                                        <a href="#" onclick="handleOrder('${ou.oid}',-6,'确定要同意退货吗？',event);">同意退货</a>
                                    </c:if>
                                    <c:if test="${ou.status==0}">
                                        <a href="#" onclick="handleOrder('${ou.oid}',-3,'确定要取消这个订单吗？',event);">取消订单</a>
                                    </c:if>
                                        <%--<c:if test="${ou.status==-6}">--%>
                                        <%--<a href="#" onclick="handleOrder('${ou.oid}',-4,'确定收到退货吗？',event);">确认收到退货</a>--%>
                                        <%--</c:if>--%>
                                    <c:if test="${ou.status==1}">
                                        <a href="#"
                                           onclick="sendGoods('${ou.oid}',2,event)">去发货</a>
                                    </c:if>
                                    <c:if test="${ou.status==3||ou.status==-3||ou.status==-6}">
                                        <a href="${pageContext.request.contextPath}/order/delOrder?oid=${ou.oid}"
                                           onclick="return confirm('确定要删除订单吗？');">删除订单</a>
                                    </c:if>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>

<c:import url="../common/page_bottom.jsp"/>

<div class="modal fade" id="postInfoFormModal" role="dialog" aria-hidden="true" aria-labelledby="myModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <form class="form-inline" role="form" id="postForm" method="post" action="">
                <div class="modal-header">
                    <button class="close" aria-hidden="true" type="button" data-dismiss="modal">×</button>
                    <h4 class="modal-title" id="myModalLabel">添加发货信息</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="orderPostname"> 快递公司名称 </label>
                        <input class="form-control" name="orderPostname" id="orderPostname" type="text"
                               placeholder="快递公司名称" required/>
                    </div>
                    <div class="form-group">
                        <label for="orderPostcode"> 快递单号</label>
                        <input class="form-control" name="orderPostcode" id="orderPostcode" type="text"
                               placeholder="快递单号" required/>
                    </div>

                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary" type="button" data-dismiss="modal" id="ok">确定</button>
                    <button class="btn btn-default" type="button" data-dismiss="modal">关闭窗口</button>
                </div>
            </form>
        </div>
    </div>
</div>

<c:import url="../common/msgModal.jsp"/>

<c:import url="../common/adminFooter.jsp"/>

</body>
</html>